﻿namespace KinonekoSoftware.Studio.Models.Modules
{
    public sealed class DegreeElementPresenter : ElementPresenter<Element_Degree>
    {
        protected override void OnInitialize(Element_Degree target)
        {
            RaiseUpdated(nameof(Metadata));
            RaiseUpdated(nameof(Value));
            RaiseUpdated(nameof(Negative));
            RaiseUpdated(nameof(Positive));
            RaiseUpdated(nameof(Color));
            base.OnInitialize(target);
        }

        /// <summary>
        /// 获得当前内容块的标题。
        /// </summary>
        /// <returns>返回当前内容块的标题。</returns>
        public override string GetTitle() => string.IsNullOrEmpty(Title) ? CodariaSR.Module_Block_Degree : Title;

        public override bool                 IsMultipleVariants()           => false;
        public override IEnumerable<Variant> GetVariants(string documentID) => null;

        public override Variant GetVariant(string documentID)
        {
            return new StringVariant
            {
                Id       = documentID,
                Metadata = Metadata,
                Value    = FinalValue,
                Type     = VariantType.String,
            };
        }
        
        public string Negative
        {
            get => AssociatedObject?.Negative;
            set
            {
                if (AssociatedObject is null)
                {
                    return;
                }

                AssociatedObject.Negative = value;
                RaisePropertyUpdated(FinalValue);
                RaiseUpdated();
            }
        }
        
        public string Positive
        {
            get => AssociatedObject?.Positive;
            set
            {
                if (AssociatedObject is null)
                {
                    return;
                }


                RaisePropertyUpdated(FinalValue);
                AssociatedObject.Positive = value;
                RaiseUpdated();
            }
        }
        
        public string Color
        {
            get
            {
                if (AssociatedObject is null)
                {
                    return "#007ACC";
                }
                return AssociatedObject.Color;
            }
            set
            {
                if (AssociatedObject is null)
                {
                    return;
                }
                
                AssociatedObject.Color = value;
                RaiseUpdated();
            }
        }

        public string Metadata
        {
            get => AssociatedObject?.Metadata;
            set
            {
                if (AssociatedObject is null)
                {
                    return;
                }


                RaiseMetadataChanged(AssociatedObject.Metadata, value);
                AssociatedObject.Metadata = value;
                RaiseUpdated();
            }
        }


        public int Fallback
        {
            get => AssociatedObject?.Fallback ?? 0;
            set
            {
                if (AssociatedObject is null)
                {
                    return;
                }

                AssociatedObject.Fallback = value;
                RaiseUpdated();
            }
        }

        public string FinalValue => Value >= 5 ? Positive : Negative;

        public int Value
        {
            get
            {
                if (AssociatedObject is null)
                {
                    return 0;
                }

                return AssociatedObject.Value;
            }
            set
            {
                if (AssociatedObject is null)
                {
                    return;
                }

                AssociatedObject.Value = value;
                RaiseUpdated();
                RaisePropertyUpdated(FinalValue);
            }
        }
    }
}